package com.prp.service;

import java.util.Date;
import java.util.Iterator;
import java.util.Set;



import com.prp.dao.Collect;
import com.prp.dao.CollectDAO;
import com.prp.dao.Comment;
import com.prp.dao.CommentDAO;
import com.prp.dao.Editlog;
import com.prp.dao.Grade;
import com.prp.dao.GradeDAO;
import com.prp.dao.Item;
import com.prp.dao.ItemDAO;
import com.prp.dao.Userinfo;
import com.prp.dao.UserinfoDAO;
import com.prp.interfaces.*;


public class FileInfoService implements ICommentServ ,ICollectServ, IGradeServ, IFileInfo{

	public int commitComm(Comment comm, String user, int itemID) {
		CommentDAO dao = new CommentDAO();
		try{
		ItemDAO dao1 = new ItemDAO();
		Item item = dao1.findById(itemID);
		UserinfoDAO dao2 = new UserinfoDAO();
		
		Userinfo userinfo = dao2.findById(user);
		dao.getSession().beginTransaction();
		
		comm.setDate(new Date());
		comm.setItem(item);
		comm.setUserinfo(userinfo);
		dao.save(comm);
		
		
		}
		catch(Exception e)
		{
			dao.getSession().getTransaction().rollback();
			dao.getSession().close();
			return -1;
		}
		dao.getSession().getTransaction().commit();
		dao.getSession().close();
		// TODO Auto-generated method stub
		return 1;
	}

	public Set getCommentList(int itemID) 
	{
		ItemDAO dao = new ItemDAO();
		Item i = dao.findById(itemID);
		
		
		// TODO Auto-generated method stub
		return i.getComments();
	}

	public int collectItem(String username, int itemID) {
		ItemDAO dao2 = new ItemDAO();
		Item item = dao2.findById(itemID);
		
		if (this.isCollect(username, item))
			return -1;
		
		CollectDAO dao = new CollectDAO();
		UserinfoDAO dao3 = new UserinfoDAO();
		Userinfo user = dao3.findById(username);
		
		dao.getSession().beginTransaction();
		Collect c = new Collect();
		c.setDate(new Date());
		c.setItem(item);
		c.setUserinfo(user);
		dao.attachDirty(c);
		
		
		dao.getSession().getTransaction().commit();
		dao.getSession().close();
		
		
		
		// TODO Auto-generated method stub
		return 0;
	}

	public int delCollect(String username, int itemID) {
		ItemDAO dao2 = new ItemDAO();
		Item item = dao2.findById(itemID);
		
		if (!this.isCollect(username, item))
			return -1;
		
		CollectDAO dao = new CollectDAO();
		UserinfoDAO dao3 = new UserinfoDAO();
		Userinfo user = dao3.findById(username);
		
		dao.getSession().beginTransaction();
		Collect c = null;
		Iterator iter = user.getCollects().iterator();
		while(iter.hasNext()){
			c = (Collect) iter.next();
			if (c.getItem().getItemId().equals(itemID))
				break;
		}
		if (c != null){
			dao.delete(c);
		}else{
			return -1;
		}
		
		
		
		
		dao.getSession().getTransaction().commit();
		dao.getSession().close();
		
		// TODO Auto-generated method stub
		return 0;
	}

	public boolean isCollect(String username, Item item) {
		UserinfoDAO dao3 = new UserinfoDAO();
		if (username == null)
			return false;
		
		Userinfo user = dao3.findById(username);
		if (user == null)
			return false;
		
		
		Iterator iter = user.getCollects().iterator();
		int a = user.getCollects().size();
		
		boolean isFind = false;
		
		while(iter.hasNext()){
			Collect c = (Collect) iter.next();
			if (c.getItem().getItemId().equals(item.getItemId()))
				isFind = true;
		}
	
		
		// TODO Auto-generated method stub
		return isFind;
	}

	public double getAvgGrade(Item item) {
		
		double grade = 0;
		int gradeNum = item.getGrades().size();
		Iterator iter = item.getGrades().iterator();
		while(iter.hasNext()){
			Grade g = (Grade) iter.next();
			grade += g.getStar();
		}
		if (gradeNum != 0)
			grade = grade / gradeNum;
		
		int z = (int) grade;
		double x = grade - z;
		if (x > 0.2 && x < 0.8)
			grade = z + 0.5;
		else
			grade = z;
		
		
		// TODO Auto-generated method stub
		return grade;
	}

	public int gradeItem(String username, int itemID, int grade) {
		ItemDAO dao1 = new ItemDAO();
		UserinfoDAO dao2 = new UserinfoDAO();
		GradeDAO dao = new GradeDAO();
		dao.getSession().beginTransaction();
		try{
			Item item = dao1.findById(itemID);
			Userinfo user = dao2.findById(username);
			Grade g = new Grade();
			g.setItem(item);
			g.setUserinfo(user);
			g.setDate(new Date());
			g.setStar(grade);
			dao.attachDirty(g);
			
		}catch(Exception e){
			dao.getSession().getTransaction().rollback();
			dao.getSession().close();
			return -1;
		}
		dao.getSession().getTransaction().commit();
		dao.getSession().flush();
		// TODO Auto-generated method stub
		return 0;
	}
	
	public Boolean isGrade(String username, Item item){
		if(username == null)
			return false;
		
		boolean isGrade =false;
		Iterator iter = item.getGrades().iterator();
		while(iter.hasNext()){
			Grade g = (Grade) iter.next();
	
			if (g.getUserinfo().getUsername().equals(username))
				isGrade = true;
		}
		
		return isGrade;
	}

	public Editlog getUploadInfo(int itemID) {
		ItemDAO dao1 = new ItemDAO();
		Item item = dao1.findById(itemID);
		
		Iterator iter = item.getEditlogs().iterator();
		Editlog e = null;
		while(iter.hasNext()){
			e = (Editlog) iter.next();
			if (e.getOperate().equals(0))
				break;
		}
		
		
		// TODO Auto-generated method stub
		return e;
	}

}
